package com.barneyx.mshinfoplatform.web.admin;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.enums.EInfoState;
import com.barneyx.mshinfoplatform.entity.info.InfoEntity;
import com.barneyx.mshinfoplatform.entity.info.InfoHistoryEntity;
import com.barneyx.mshinfoplatform.exception.InfoException;
import com.barneyx.mshinfoplatform.exception.MshException;
import com.barneyx.mshinfoplatform.service.info.IInfoHistoryEntityService;
import com.barneyx.mshinfoplatform.util.InfoUtil;
import com.barneyx.mshinfoplatform.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@RestController
@RequestMapping("/api/admin/info/")
@Slf4j
public class AdminInfoController {
    @Resource
    private InfoUtil.Info infoUtil;

    @Resource
    private IInfoHistoryEntityService infoHistoryService;

    @GetMapping("page")
    public ExResult<?> page(@RequestParam(value = "cateid", required = false,defaultValue = "") String cateid,
                            @RequestParam(value = "userid", required = false, defaultValue = "") String userid,
                            @RequestParam(value = "keyword", required = false, defaultValue = "") String keyword,
                            @RequestParam(value = "current", defaultValue = "1", required = false) long current,
                            @RequestParam(value = "size", defaultValue = "20", required = false) long size) throws MshException {
        return ExResult.ofSuccess(infoUtil.getPage2(current, size, new LambdaQueryWrapper<InfoEntity>()
                .func(StrUtil.isAllBlank(cateid, keyword), e -> {
                })
                .func(StrUtil.isBlank(cateid) && StrUtil.isNotBlank(keyword), info -> info.likeLeft(InfoEntity::getRemark, keyword.trim()))
                .func(StrUtil.isNotBlank(cateid) && StrUtil.isBlank(keyword), info -> info.eq(InfoEntity::getInfoCateId, cateid))
                .func(StrUtil.isAllNotBlank(cateid, keyword), info -> info.likeLeft(InfoEntity::getRemark, keyword).eq(InfoEntity::getInfoCateId, cateid))
                )

        );
    }


    @GetMapping("get-by-id")
    public ExResult<?> getById(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoUtil.getInfoService().getById(id));
    }

    @DeleteMapping("delete-by-id")
    public ExResult<?> removeById(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoUtil.getInfoService().removeById(id));
    }

    @PutMapping("update-info")
    public ExResult<?> updateInfo(@RequestBody InfoEntity info) throws InfoException {
        return ExResult.ofAuto(infoUtil.updateInfoBaseInfo(info), info);
    }

    @PostMapping("create-info")
    public ExResult<?> createInfo(@RequestBody InfoEntity info) throws MshException {
        return ExResult.ofAuto(infoUtil.createInfo(info), info);
    }

    @GetMapping("history-list-by-info-id")
    public ExResult<?> getHistoryListByInfoId(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoHistoryService.list(new LambdaQueryWrapper<InfoHistoryEntity>()
                .eq(InfoHistoryEntity::getInfoId, id)));
    }


    /**
     * 设置通过审核
     *
     * @param id
     * @return
     */
    @GetMapping("pass-ok")
    public ExResult<?> PassInfoOk(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoUtil.InfoSH(id, EInfoState.PASS));
    }

    @GetMapping("change-state")
    public ExResult<?> ChangeState(@RequestParam("id") String id,
                                   @RequestParam("state") EInfoState state) {
        return ExResult.ofAuto(infoUtil.InfoSH(id, state));
    }

    @PutMapping("change-state")
    public ExResult<?> ChangeStateByPut(@RequestBody JSONObject data) {
        String id = data.getString("id");
        EInfoState state = data.getObject("state", EInfoState.class);
        String errorMsg = data.getString("msg");
        return ExResult.ofAuto(infoUtil.InfoSH(id, state, errorMsg));
    }

    /**
     * 设置未通过审核
     *
     * @param id
     * @return
     */
    @GetMapping("pass-faild")
    public ExResult<?> PassFaild(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoUtil.InfoSH(id, EInfoState.NOTPASS));
    }

    /**
     * 设置指定的违规信息
     *
     * @param id
     * @return
     */
    @GetMapping("info-violations")
    public ExResult<?> infoViolations(@RequestParam("id") String id) {
        return ExResult.ofAuto(infoUtil.InfoSH(id, EInfoState.VIOLATIONS));
    }


    @GetMapping("info-count-report")
    public ExResult<?> getCountReport() throws MshException {
        DateTime beginOfDay = DateUtil.beginOfDay(new Date());
        DateTime endOfDay = DateUtil.endOfDay(new Date());
        log.info(beginOfDay.toString());
        log.info(endOfDay.toString());
        //获取今日发布信息数据
        long toDayCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()))
        );
        //昨天发布量
        long yestoDayCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, DateUtil.beginOfDay(DateUtil.yesterday()), DateUtil.endOfDay(DateUtil.yesterday()))
        );


        //本周
        long toWeekCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, DateUtil.beginOfWeek(new Date()), DateUtil.endOfWeek(new Date()))
        );

        //上周
        long lastWeekCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, DateUtil.beginOfWeek(DateUtil.lastWeek()), DateUtil.endOfWeek(DateUtil.lastWeek()))
        );

        //上月
        long lastMonthCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, DateUtil.lastMonth(), Util.currentMonthStart())
        );
        //本月
        long monthCount = infoUtil.getInfoService().count(new LambdaQueryWrapper<InfoEntity>()
                .eq(InfoEntity::getAreaId, infoUtil.getAreaUtil().getCurrentAreaId())
                .between(InfoEntity::getCreateAt, Util.currentMonthStart(), Util.currentMonthEnd())
        );
        return ExResult.ofSuccess(MapUtil.builder(new HashMap<String, Object>())
                .put("today", toDayCount)
                .put("yestday", yestoDayCount)
                .put("toweek", toWeekCount)
                .put("lastweek", lastWeekCount)
                .put("month", monthCount)
                .put("lastmonth", lastMonthCount)
                .build());
    }


}
